﻿Imports System.Windows.Forms
Imports System.Xml
Public Class Dialog2
    Private Sub Dialog2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadBookmarks()
    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
       
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Public Sub LoadBookmarks()
        SiteList.Items.Clear()
        Dim rdrXML As New XmlTextReader(My.Settings.Directory & "\WebSites.xml")
        rdrXML.MoveToContent()
        Dim ElementName As String = ""
        Dim NextItem As Boolean = True
        Dim objListViewItem As ListViewItem = Nothing

        Do While rdrXML.Read
            If NextItem Then
                objListViewItem = New ListViewItem
                NextItem = False
            End If
            Select Case rdrXML.NodeType
                Case XmlNodeType.Element
                    ElementName = rdrXML.Name
                Case XmlNodeType.Text
                    If ElementName = "Name" Then
                        objListViewItem.Text = rdrXML.Value
                    End If
                    If ElementName = "URL" Then
                        objListViewItem.SubItems.Add(rdrXML.Value)
                        SiteList.Items.Add(objListViewItem)
                        NextItem = True
                    End If
            End Select
        Loop
        rdrXML.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NewMark As New ListViewItem

        NewMark.Text = TextBox1.Text
        NewMark.SubItems.Add(TextBox2.Text)

        SiteList.Items.Add(NewMark)
        WriteToXML()
    End Sub

    Public Sub WriteToXML()
        Dim wtrXML As New XmlTextWriter(My.Settings.Directory & "\WebSites.xml", System.Text.Encoding.UTF8)
        With wtrXML
            .Formatting = Formatting.Indented
            .WriteStartDocument()
            .WriteStartElement("WebSites")
            Dim objListViewItem As New ListViewItem
            For Each objListViewItem In SiteList.Items
                .WriteStartElement("WebSite")
                .WriteElementString("Name", objListViewItem.Text)
                .WriteElementString("URL", objListViewItem.SubItems(1).Text)
                .WriteEndElement()
            Next
            .WriteEndElement()
            .WriteEndDocument()
            .Flush()
            .Close()
        End With
    End Sub
End Class
